﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Publishing;
using Microsoft.SharePoint.WebControls;

namespace SPHelper
{
	public class Feature
	{

		/// <summary>
		/// Get feature by feature name
		/// </summary>
		/// <param name="featureName"></param>
		/// <returns></returns>
		public static Microsoft.SharePoint.Administration.SPFeatureDefinition GetFeature(string featureName)
		{
			Microsoft.SharePoint.Administration.SPFeatureDefinition feature = null;
			try
			{
				// init
				if (string.IsNullOrEmpty(featureName)) { throw new Exception("Feature name passed is null"); }
				featureName = featureName.ToLower();
				System.Globalization.CultureInfo cultureInfo = new System.Globalization.CultureInfo(1033);

				foreach (Microsoft.SharePoint.Administration.SPFeatureDefinition f in Microsoft.SharePoint.Administration.SPFarm.Local.FeatureDefinitions)
				{
					if (f.GetTitle(cultureInfo).ToLower() == featureName || (!string.IsNullOrEmpty(f.DisplayName) && f.DisplayName.ToLower() == featureName))
					{
						feature = f;
						break;
					}
				}
			}
			catch (Exception ex)
			{
				SPHelper.Log.WriteError("xdgfhcgjtfdzstdzsdfgv", ex, "feature: " + featureName);
			}
			return feature;
		}

		///// <summary>
		///// Activate feature at site scope
		///// If duplicate already exists, then skips
		///// </summary>
		///// <param name="site"></param>
		///// <param name="featureId"></param>
		///// <param name="isSave"></param>
		///// <returns></returns>
		//public static bool ActivateFeature(SPSite site, Guid featureId, bool isSave)
		//{
		//    bool isSuccessful = false;
		//    try
		//    {
		//        // init
		//        if (site == null) { throw new Exception("SPSite passed is null"); }
		//        if (featureId == null || featureId == Guid.Empty) { throw new Exception("FeatureId GUID passed is null or empty"); }


		//        bool isMatch = false;
		//        foreach (SPFeature feature in site.Features)
		//        {
		//            if (feature.DefinitionId == featureId) { isMatch = true; }
		//        }
		//        if (!isMatch) { site.Features.Add(featureId); }
		//        else if (isSave) { isSave = false; }

		//        // save
		//        if (isSave) { site.upda; }

		//        // assume success
		//        isSuccessful = true;
		//    }
		//    catch (Exception ex)
		//    {
		//        SPHelper.Log.WriteError("fdhyjgdyttrtsr", ex, "featureId: " + featureId);
		//        isSuccessful = false;
		//    }
		//    return isSuccessful;
		//}

		/// <summary>
		/// Activate a feature at web scope
		/// If duplicate already exists, then skips
		/// </summary>
		/// <param name="web"></param>
		/// <param name="featureId"></param>
		/// <param name="isSave"></param>
		/// <returns></returns>
		public static bool ActivateFeature(SPWeb web, Guid featureId, bool isSave)
		{
			bool isSuccessful = false;
			try
			{
				// init
				if (web == null) { throw new Exception("SPWeb passed is null"); }
				if (featureId == null || featureId == Guid.Empty) { throw new Exception("FeatureId GUID passed is null or empty"); }


				bool isMatch = false;
				foreach (SPFeature feature in web.Features)
				{
					if (feature.DefinitionId == featureId) { isMatch = true; }
				}
				if (!isMatch) { web.Features.Add(featureId); }
				else if (isSave) { isSave = false; }

				// save
				if (isSave) { web.Update(); }

				// assume success
				isSuccessful = true;
			}
			catch (Exception ex)
			{
				SPHelper.Log.WriteError("r85yftydsrserwer", ex, "featureId: " + featureId);
				isSuccessful = false;
			}
			return isSuccessful;
		}
	}
}
